package ckw.gui.wizards;

import java.util.ArrayList;
import java.util.HashSet;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;

import ckw.database.DatabaseManager;
import ckw.database.OKW;
import ckw.database.OKWMember;

public class WzdElectionPg2 extends WizardPage {
	private ArrayList<OKW> okwList;
	private Tree tree;

	private Button btnWybierzCzlonkow;

	/**
	 * Create the wizard.
	 */
	public WzdElectionPg2() {
		super("wizardPage");
		setTitle("Dodaj wybory");
		setDescription("Krok 2. Wybierz OKW i ich czlonkow");
		DatabaseManager db = DatabaseManager.getInstance();

		okwList = (ArrayList<OKW>) db.getAll(OKW.class);

		for (int i = 0; i < okwList.size(); i++) {
			OKW okw = okwList.get(i);
			okw = db.get(OKW.class, okw.getId());
			Object[] okwMemberList = okw.getOkwMembers().toArray();
			okw.setOkwMembers(new HashSet<OKWMember>());
			for (int j = 0; j < okwMemberList.length; j++) {
				OKWMember okwMember = (OKWMember) okwMemberList[j];
				okwMember = db.get(OKWMember.class, okwMember.getId());
				okw.addToOkwMember(okwMember);
			}
			okwList.set(i, okw);
		}
	}

	/**
	 * Create contents of the wizard.
	 * 
	 * @param parent
	 */
	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);

		setControl(container);
		container.setLayout(new GridLayout(2, false));

		tree = new Tree(container, SWT.BORDER);
		tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 2));
		tree.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent arg0) {
				TreeItem trtmSelected = tree.getSelection()[0];
				btnWybierzCzlonkow
						.setEnabled(trtmSelected.getData().getClass() == OKW.class);
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
			}
		});

		Button btnWybierzOkw = new Button(container, SWT.CENTER);
		btnWybierzOkw.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false,
				false, 1, 1));
		btnWybierzOkw.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				WzdElectionDlgOKWSelect dlg = new WzdElectionDlgOKWSelect(getShell(),
						okwList);
				if (dlg.open() == IDialogConstants.OK_ID) {
					UpdateTree();
					btnWybierzCzlonkow.setEnabled(false);
				}
			}
		});
		btnWybierzOkw.setText("Wybierz OKW");

		btnWybierzCzlonkow = new Button(container, SWT.CENTER);
		btnWybierzCzlonkow.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				OKW okw = (OKW) tree.getSelection()[0].getData();
				WzdElectionDlgOKWMemberSelect dlg = new WzdElectionDlgOKWMemberSelect(
						getShell(), okw);
				
				if (dlg.open() == IDialogConstants.OK_ID) {
					UpdateTree();
					btnWybierzCzlonkow.setEnabled(false);
				} 
			}
		});
		btnWybierzCzlonkow.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false,
				false, 1, 1));
		btnWybierzCzlonkow.setText("Wybierz czlonkow");
		btnWybierzCzlonkow.setEnabled(false);
	}

	protected void UpdateTree() {
		tree.removeAll();
		for (OKW okw : okwList) {
			if (okw.getChecked() == true) {
				TreeItem trtmOKW = new TreeItem(tree, SWT.NONE);
				trtmOKW.setText(okw.getName());
				trtmOKW.setExpanded(true);
				trtmOKW.setData(okw);

				for (OKWMember okwMember : okw.getOkwMembers()) {
					if (okwMember.getChecked() == true) {
						TreeItem trtmOKWMember = new TreeItem(trtmOKW, SWT.NONE);
						trtmOKWMember.setText(okwMember.getName());
						trtmOKWMember.setData(okwMember);
					}
				}
				tree.redraw();
			}
		}
	}

	public ArrayList<OKW> getElectionOkwList() {
		return okwList;
	}
}
